# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

from group import group
from passport import passport
from groupList import groupList
from passList import passList

class listManager:
	def __init__(self, ui):
		self.ui = ui
		self.gList = ''
		self.pList = ''
		self.default_group = 0
		self.default_user = -1
	
	def rebuild(self):
		self.make_List()

	def userBlocked(self, email):
		if int(self.ui.mam.config['show_groups']) == 1:
			for g in self.ui.mam.contactListManager.gList.grupos:
				for p in g.passports.passports:
					if email == p.name:
						return p.blocked
		else:
			for p in self.ui.mam.contactListManager.pList.passports:
				if email == p.name:
					return p.blocked

	def userOnline(self, email):
		# to see if user got disconnected
		status = 'NFLN'
		if int(self.ui.mam.config['show_groups']) == 1:
			for g in self.ui.mam.contactListManager.gList.grupos:
				found = 0
				for p in g.passports.passports:
					if email == p.name and p.status == 'FLN':
						found = 1
						status = 'FLN'
						break
				if found:
					break
		else:
			for p in self.ui.mam.contactListManager.pList.passports:
				if email == p.name and p.status == 'FLN':
					status = 'FLN'
					break
		if status == 'FLN':
			return 0
		else:
			return 1

	# This creates our contact list based on msnlib one.
	# It keeps all our contacts, their states and other important values.
	def make_List(self):
		self.gList = groupList()
		gids = self.ui.mam.m.groups.keys()
		gids.sort()
		# Create groups
		for g in gids:
			g1 = group()
			g1.name = self.ui.mam.m.groups[g]
			g1.gid = g
			g1.expanded = 0
			self.gList.appendGroup(g1)
		users = self.ui.mam.m.users.keys()
		users.sort()
		# Create only list of passports or add contacts to groups 
		if not int(self.ui.mam.config['show_groups']):
			self.default_user = 0		
			self.pList = passList()			
			for c in users:
				p1 = passport()
				p1.name = c
				p1.status = self.ui.mam.m.users[c].status
				p1.realnick = self.ui.mam.m.users[c].realnick
				if c in self.ui.mam.aliasesList.keys():
					p1.nick = self.ui.mam.aliasesList[c]
				else:
					p1.nick = self.ui.mam.m.users[c].nick
				p1.gid = self.ui.mam.m.users[c].gid
				self.ui.mam.contactListManager.pList.append(p1)				
				if 'B' in self.ui.mam.m.users[p1.name].lists:	# Check if user is blocked
					p1.blocked = 1
		else:
			self.default_group = 0
			self.default_user = -1
			for c in users:
				p1 = passport()
				p1.name = c
				p1.status = self.ui.mam.m.users[c].status				
				p1.realnick = self.ui.mam.m.users[c].realnick
				if c in self.ui.mam.aliasesList.keys():
					p1.nick = self.ui.mam.aliasesList[c]
				else:
					p1.nick = self.ui.mam.m.users[c].nick
				p1.gid = self.ui.mam.m.users[c].gid
				self.gList.appendpassport(p1)
				# Check if user is blocked
				if 'B' in self.ui.mam.m.users[p1.name].lists:
					p1.blocked = 1
			# Check if all users in groups are blocked
			for g in self.gList.grupos:
				if g.checkifBlocked():
					g.blocked = 1

	# This method is used for moving 
	# default_user index and/or default_group index
	# when moving around interface contact list
	def moveDown(self):
		show_offline = int(self.ui.mam.config['show_offline'])
		show_groups = int(self.ui.mam.config['show_groups'])
		#a. If organized by groups
		if show_groups:
			size_grupo = self.gList.grupos[self.default_group].passports.size()
			if not show_offline:
				num_non_fln = self.gList.grupos[self.default_group].passports.countNonFLN()
			else:
				num_non_fln = size_grupo
			num_grupos = self.gList.size()
			expanded = self.gList.grupos[self.default_group].expanded
				# If I am over a group
			if (self.default_user == -1) and (self.ui.padArray.idactive == 0):
				# It is not expanded
				if self.gList.grupos[self.default_group].expanded == 0:
					self.default_group += 1
					self.default_user = -1
				# expanded, show_offline = 0, and online contacts on the group
				elif (expanded == 1) and (not show_offline) and (num_non_fln <> 0):
					self.default_user += 1
				elif (expanded == 1) and (show_offline) and (size_grupo <> 0):
					self.default_user += 1
				else:
					self.default_user = -1
					self.default_group += 1
			# Last contact of a group
			elif self.ui.padArray.idactive == 0 and self.default_user == num_non_fln - 1:
				self.default_group += 1
				self.default_user = -1
			# Over a contact
			else:
				self.default_user += 1
		#b. Don't organized by groups
		else:
			if self.default_user == self.pList.size() - 1:
				pass
			else:
				self.default_user += 1

	# This method is used by the interface manager for moving 
	# default_user index and/or default_group index
	# when moving around contact list
	def moveUp(self):
		show_offline = int(self.ui.mam.config['show_offline'])
		show_groups = int(self.ui.mam.config['show_groups'])

		# a. If organized by groups
		if show_groups:
			num_grupos = self.gList.size()
			expanded = self.gList.grupos[self.default_group].expanded
			if self.default_group <> 0:
				expanded_prev = self.gList.grupos[self.default_group - 1].expanded
				size_previous_group = self.gList.grupos[self.default_group-1].passports.size()
			else:
				expanded_prev = 0
			num_non_fln = self.gList.grupos[self.default_group].passports.countNonFLN()
			size_grupo = self.gList.grupos[self.default_group].passports.size()
			
			# If I am over a group
			if self.default_user == -1 and self.ui.padArray.idactive == 0:
				num_non_fln_prev = self.gList.grupos[self.default_group - 1].passports.countNonFLN()
				if expanded_prev == 1 and not show_offline and num_non_fln_prev <> 0:
					self.default_group -= 1
					size_grupo = self.gList.grupos[self.default_group].passports.countNonFLN()
					self.default_user = size_grupo - 1
				elif expanded_prev == 1 and show_offline and size_previous_group <> 0:
					self.default_group -= 1
					size_grupo = self.gList.grupos[self.default_group].passports.size()
					self.default_user = size_grupo - 1
				else:
					self.default_group -= 1
					#default_user stays in -1
			# If I am over a contact
			else:
				self.default_user -= 1
		# b. Don't organised by groups
		else:
			if self.default_user == 0:
				pass
			else:
				self.default_user -= 1
